package com.hmdp.Interceptor;

import com.hmdp.utils.UserHolder;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@Slf4j
public class LoginInterceptor implements HandlerInterceptor {
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        //1.判断是否需要拦截(ThreadLocal中是否有用户)
        log.info("Checking user in LoginInterceptor: {}", UserHolder.getUser());
        if(UserHolder.getUser() == null){
            log.info("登录拦截器里拦截----------------------");
            response.setStatus(401);

            return false;
        }
        log.info("登录拦截器里放行-----------------------");
        return true;
    }
}
